<?php
// 学生信息：[您的全名] [学生证号]
// 功能：产品列表与CRUD操作入口
session_start();
if (!isset($_SESSION['logged_in'])) {
    header('Location: login.php');
    exit;
}

// 加载产品数据
$jsonFile = '../data/products.json';
$products = json_decode(file_get_contents($jsonFile), true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Product Management</title>
    <style>
        /* 复用主题样式，保持一致性 */
        /* 页眉样式 - 与WordPress一致 */
.header {
    background: url('head.jpg') center/cover no-repeat; /* 注意路径是否正确，若head.jpg在admin同级目录需加../ */
    padding: 20px 0;
    position: relative;
}
.header-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.logo {
    height: 40px; /* 推荐缩小为40px，更协调 */
    max-width: 100%;
    width: auto;
    object-fit: contain;
}
        .admin-container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }
        .action-buttons {
            margin: 20px 0;
        }
        .btn {
            display: inline-block;
            padding: 8px 15px;
            text-decoration: none;
            border-radius: 4px;
            margin-right: 10px;
        }
        .btn-add {
            background-color: #4CAF50;
            color: white;
        }
        .btn-edit {
            background-color: #2196F3;
            color: white;
        }
        .btn-delete {
            background-color: #f44336;
            color: white;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f5f5f5;
        }
    </style>
</head>
<body>
    <header class="header">
        <div class="header-content">
            <img src="../logo.jpg" alt="Fezco Logo" class="logo">
            <nav class="nav">
                <a href="../products.php">View Store</a>
                <a href="logout.php">Logout</a>
            </nav>
        </div>
    </header>

    <div class="admin-container">
        <h1>Product Management</h1>
        <div class="action-buttons">
            <a href="add.php" class="btn btn-add">Add New Product</a>
        </div>
        <table>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Price</th>
                <th>Category</th>
                <th>Actions</th>
            </tr>
            <?php foreach ($products as $product) { ?>
            <tr>
                <td><?php echo $product['id']; ?></td>
                <td><?php echo $product['name']; ?></td>
                <td>$<?php echo $product['price']; ?></td>
                <td><?php echo $product['category']; ?></td>
                <td>
                    <a href="edit.php?id=<?php echo $product['id']; ?>" class="btn btn-edit">Edit</a>
                    <a href="delete.php?id=<?php echo $product['id']; ?>" class="btn btn-delete" onclick="return confirm('Are you sure?')">Delete</a>
                </td>
            </tr>
            <?php } ?>
        </table>
    </div>
</body>
</html>